package cn.tedu.sims.web;

import cn.tedu.sims.ex.ServiceException;
import lombok.Data;

import java.io.Serializable;

/**
 *JSON响应格式
 */
@Data
public class JsonResult <T> implements Serializable {
    //状态码
    private Integer state;
    //操作失败时的描述文本
    private String message;
    //操作成功返回的数据
    private T data;

    public static JsonResult<Void> ok(){
            return ok(null);
    }
    public static<T> JsonResult<T>ok(T data){
        JsonResult<T> jsonResult=new JsonResult<>();
        jsonResult.state=ServiceCode.OK.getValue();
        jsonResult.data=data;
        return jsonResult;
    }
    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }
    public static JsonResult<Void> fail(ServiceCode serviceCode,String message){
        JsonResult<Void> jsonResult=new JsonResult<>();
        jsonResult.message=message;
        jsonResult.state=serviceCode.getValue();
        return jsonResult;
    }
}
